
import logging
from logging.handlers import RotatingFileHandler

def setup_logger(log_file='log/all.log'):
    # 创建日志格式化器
    fmt = '%(asctime)s %(levelname)s [%(name)s]  [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'
    formatter = logging.Formatter(fmt)

    # 创建日志处理器（支持日志轮转）
    handler = RotatingFileHandler(
        log_file,
        maxBytes=1024*1024,  # 1MB
        backupCount=5,
        encoding='utf-8'
    )
    handler.setFormatter(formatter)

    # 获取根日志器并配置
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    logger.handlers.clear()
    logger.addHandler(handler)
    return logger

# 初始化日志（项目启动时调用一次）
try:
    logger = setup_logger()
except Exception as e:
    print(f"日志初始化失败: {e}")
    raise
